package net.csdn.business.notify.api.config;

import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import java.util.Properties;

/**
 * @ClassName MqConfig
 * @Description rocketmq配置
 * @Author zhangyl
 * @Date 2023/5/5 18:44
 */
@Data
@Configuration
@RefreshScope
public class MqConfig {
    private static final String DEFAULT_MAX_TIMES = "5";
    private static final String DEFAULT_THREAD_NUMS = "5";

    @Value("${ali.mq.accessKey}")
    private String accessKey;

    @Value("${ali.mq.secretKey}")
    private String secretKey;

    @Value("${ali.mq.tcp.nameSrvAddr}")
    private String tcpNameSrvAddr;

    @Value("${ali.mq.notify.groupId:GID_WEB_NOTIFY_API}")
    private String notifyApiGroupId;

    @Value("${ali.mq.instanceId}")
    private String instanceId;

    @Value("${ali.mq.notify.remind.topic:NOTIFY_REMIND_CHANNEL}")
    private String remindTopic;

    @Value("${ali.mq.notify.private.topic:NOTIFY_PRIVATE_CHANNEL}")
    private String privateTopic;

    @Value("${ali.mq.consumer.max:5}")
    private String consumerMaxTimes;
    @Value("${ali.mq.consumer.thread.num:5}")
    private String consumerThreadNums;


    /*@DependsOn("remindMessageListener")
    @Bean(name = "remindMessageListener", initMethod = "start", destroyMethod = "shutdown")
    public ConsumerBean initAliProjectChangeConsumer(RemindMessageListener remindMessageListener) {
        ConsumerBean webhookEventConsumer = new ConsumerBean();
        Properties properties=getMqProperties();
        properties.setProperty(PropertyKeyConst.GROUP_ID, webhookGroupId);
        properties.setProperty(PropertyKeyConst.MaxReconsumeTimes, Optional.ofNullable(consumerMaxTimes).orElse(DEFAULT_MAX_TIMES));
        // 默认 20-64
        properties.setProperty(PropertyKeyConst.ConsumeThreadNums, Optional.ofNullable(consumerThreadNums).orElse(DEFAULT_THREAD_NUMS));
        webhookEventConsumer.setProperties(properties);
        //订阅关系
        Map<Subscription, MessageListener> subscriptionTable = Maps.newHashMap();
        Subscription subscription = new Subscription();
        subscription.setTopic(webhookTopic);
        //subscription.setExpression(projectTag);
        subscriptionTable.put(subscription, webhookMessageListener);
        webhookEventConsumer.setSubscriptionTable(subscriptionTable);
        return webhookEventConsumer;
    }
*/

    @Bean(name = "aliProducer", initMethod = "start", destroyMethod = "shutdown")
    public ProducerBean buildReportProducer() {
        ProducerBean producer = new ProducerBean();
        Properties properties = getMqProperties();
        producer.setProperties(properties);
        return producer;
    }

    private Properties getMqProperties() {
        Properties properties = new Properties();
        properties.setProperty(PropertyKeyConst.AccessKey, this.accessKey);
        properties.setProperty(PropertyKeyConst.SecretKey, this.secretKey);
        properties.setProperty(PropertyKeyConst.NAMESRV_ADDR, this.tcpNameSrvAddr);
        return properties;
    }

}